import os, sys
import glob

class BaseProvider(object):
  pass

from yapsy.PluginManager import PluginManager
class Provider(PluginManager):
  """
  """
  providers = {}
  
  def __init__(self):
    Provider.providers = {}
    PluginManager.__init__(self, categories_filter={"Provider": BaseProvider}, plugin_info_ext="provider")
    path = os.path.join(os.path.abspath(os.path.dirname(__file__)),"providers")
    self.setPluginPlaces([path])
    
    self.locatePlugins()
    self.loadPlugins()
    
    for plugin in self.getAllPlugins():
      for format in plugin.plugin_object.handled_types:
        self.providers[format] = plugin.plugin_object


  def checkout(self, sourceURI, targetURI, aCallBack):
    format = sourceURI.split('://')[0]+'://'
    if format in self.providers:
      return self.providers[format].checkout(sourceURI, targetURI, aCallBack) 
    else:
      print("ERROR: no provider for %s (file: %s)"%(format, sourceURI))
      return False
    
  def update(self, sourceURI, targetURI, aCallBack):
    format = sourceURI.split('://')[0]+'://'
    if format in self.providers:
      return self.providers[format].update(sourceURI, targetURI, aCallBack) 
    else:
      print("ERROR: no provider for %s (file: %s)"%(format, sourceURI))
      return False
      
  def scan(self, sourceURI, targetURI, aCallBack):
    format = sourceURI.split('://')[0]+'://'
    if format in self.providers:
      return self.providers[format].scan(sourceURI, targetURI, aCallBack) 
    else:
      print("ERROR: no provider for %s (file: %s)"%(format, sourceURI))
      return False
